﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Reflection;
using DirectUI.Mobile.UI;
using System.Windows.Forms;
namespace DirectUI.Mobile.UI.Controls
{
 public  class AutoComplete:DirectUIPanel
    {
     private IList<string> advices;
     public IList<string> Advices
     {
         get
         {
             return advices;
         }
         set
         {
             if (sp.Controls.Count < value.Count)
             {
                 for (int i = 0; i < value.Count; i++)
                 {
                     if (i >= sp.Controls.Count)
                     {
                         DirectUILabel lb = new DirectUILabel(value[i], 0, originBounds.Height * i, this.Width -20, 20);
                         lb.Click += new EventHandler(lb_Click);
                         sp.Controls.Add(lb);

                     }
                     else
                     {
                         ((DirectUILabel)sp.Controls[i]).Text = value[i];
                     }
                 }
             }
             else
             {
                 for (int i = 0; i < sp.Controls.Count; i++)
                 {
                     if (i >= value.Count)
                     {
                         ((DirectUILabel)sp.Controls[i]).Text = "";
                         if (i > 20)
                         {
                             sp.Controls.Remove(sp.Controls[i]);
                         }
                     }
                     else
                     {
                         ((DirectUILabel)sp.Controls[i]).Text = value[i];
                     }
                 }
             }
             Refresh();

         }
     }

     void lb_Click(object sender, EventArgs e)
     {
         tb.Text = ((DirectUILabel)sender).Text;
         HideAdvice();
     }
     //private DirectUIButton _bt;
     private ScrollPanel sp;
     private DirectUITextBox tb;
     public AutoComplete()
     {
         advices = new List<string>();
         this.Height = 20;
         this.Width = 100;
         //_bt = new DirectUIButton();
         //_bt.Image = Resource.combobox_arrow_down;
         sp = new ScrollPanel();
         tb = new DirectUITextBox();
         tb.Bounds = new System.Drawing.Rectangle(0, 0, this.Width , 20);
         //_bt.Bounds = new System.Drawing.Rectangle(tb.Bounds.Right, 0, 20, 20);
         sp.Bounds = new System.Drawing.Rectangle(1, 20, this.Width-2, 100);
         sp.Visible = false;
         sp.ScrollBarWidth = 20;
         sp.ScrollBarButtonColor = Color.Gray;
         sp.ScrollReverse = true;
         sp.MovintSpeed = 5;
         //_bt.Click += new EventHandler(_bt_Click);
         this.Controls.Add(tb);
         //this.Controls.Add(_bt);
         this.Controls.Add(sp);
         originBounds = this.Bounds;
         tb.GotFocus += new EventHandler(_tb_GotFocus);
         tb.TextChanged += new EventHandler(_tb_TextChanged);
         timer = new Timer();
         timer.Interval = 1000;
         timer.Enabled = false;
         timer.Tick += new EventHandler(_timer_Tick);
     }
     public override bool OnClick(PointEventArgs p)
     {
       
         if (!sp.Visible)
         {
             ShowAdvices();
         }
         return base.OnClick(p);
     }
     public override void OnGesture(GestureEventArgs e)
     {
         base.OnGesture(e);
         //if (Focused)
         //{
             //  if (!e.Handled)   // always check for left and right and cancel even if it was already handled.
             //{
                 switch (e.Gesture)
                 {
                     case Gesture.Left:
                     case Gesture.Right:
                         e.Handled = true;
                         e.Gesture = Gesture.None;
                         break;
                 }
             //}
         //}
     }
     protected override void OnSizeChanged(Size oldSize, Size newSize)
     {
         base.OnSizeChanged(oldSize, newSize);
         if (tb != null && sp != null)
         {
             tb.Bounds = new System.Drawing.Rectangle(0, 0, this.Width, 20);
             //_bt.Bounds = new System.Drawing.Rectangle(tb.Bounds.Right, 0, 20, 20);
             sp.Bounds = new System.Drawing.Rectangle(1, 20, this.Width - 2, 100);
         }
     }
     void _timer_Tick(object sender, EventArgs e)
     {
         timer.Enabled = false;
         if (ServiceMethod == null)
         {
             return;
         }
         //if (!string.IsNullOrEmpty(ServiceMethodAssemblyName))
         //{
         //    throw new NotImplementedException();
         //}
         //MethodInfo mInfo=this.Container.GetType().GetMethod(
         Advices = ServiceMethod(tb.Text);
         sp.TopOffset = 0;
      
     }
     private Timer timer = new Timer();
     private DateTime lastGetAdviceTime = new DateTime(1900, 1, 1);

     void _tb_TextChanged(object sender, EventArgs e)
     {
         //if (lastGetAdviceTime.Year == 1900)
         //{
         //    lastGetAdviceTime = DateTime.Now;
         //}
         //else
         //{
         //    TimeSpan = DateTime.Now - lastGetAdviceTime;
         //    if(
         //}
         //if (ServiceMethod == null)
         //{
         //    return;
         //}
         //if (!string.IsNullOrEmpty(ServiceMethodAssemblyName))
         //{
         //    throw new NotImplementedException();
         //}
         //MethodInfo mInfo=this.Container.GetType().GetMethod(
         //Advices = ServiceMethod(tb.Text);
         if (!timer.Enabled)
         {
             timer.Enabled = true;
         }
         if (!sp.Visible)
         {
             ShowAdvices();
         
         }
         OnPropertyChanged(new DirectUI.Mobile.UI.PropertyChangingEventArgs("Text"));
         OnTextChanged(EventArgs.Empty);
     }
     public string Text
     {
         get
         {
             return tb.Text;
         }
         set
         {
             tb.Text = value;
         }
     }

     void _tb_GotFocus(object sender, EventArgs e)
     {
        
         
         if (!sp.Visible)
         {
             if (string.IsNullOrEmpty(tb.Text))
             {
                 Advices = ServiceMethod(tb.Text);
             }
             ShowAdvices();
             tb.Focus();
         }
     }
     public override void OnLostFocus()
     {
         base.OnLostFocus();
         tb.OnLostFocus();
         HideAdvice();
     }
     private Rectangle originBounds;
     private void HideAdvice()
     {
         sp.Visible = false;
         //_bt.Image = Resource.combobox_arrow_down;
         this.Bounds = originBounds;
       
     }
     private void ShowAdvices()
     {
         //_bt.Image = Resource.combobox_arrow_up;
         originBounds = this.bounds;
         if (sp.Controls.Count > 5)
         {
             sp.Height = this.Height * 5 - 1;
             sp.AlwaysShowScrollBar = true;
             this.Height = this.Height * 6;
         }
         else
         {
             sp.Height = sp.Controls.Count * this.Height - 1;
             sp.AlwaysShowScrollBar = false;
             this.Height = sp.Height + this.Height + 1;
         }
         sp.Visible = true;
         sp.TopOffset = 0;
         if (this.Parent != null)
         {
             DirectUIControlCollection fc = (DirectUIControlCollection)((IMultiControlContainer)this.Parent).Controls;
             fc.Remove(this);
             fc.Add(this);
         }
     }
     void _bt_Click(object sender, EventArgs e)
     {
         if (sp.Visible)
         {
             HideAdvice();
         }
         else
         {
             ShowAdvices();
         }
         Refresh();
     }
     public override void OnPaint(DirectUIPaintEventArgs e)
     {
         base.OnPaint(e);
         if (sp.Visible)
         {
             e.Graphics.DrawRectangle(new Pen(Color.Black), new Rectangle(e.ControlBounds.Left, e.ControlBounds.Top + originBounds.Height, e.ControlBounds.Width - 1, e.ControlBounds.Height - originBounds.Height-1));

         }
     }

     /// <summary>
     /// Occurs when the text property is changed.
     /// </summary>
     protected virtual void OnTextChanged(EventArgs e)
     {
       
         if (TextChanged != null) TextChanged(this, EventArgs.Empty);
     }

     public event EventHandler TextChanged;


     public delegate IList<string> AutoCompleteServiceMethod(string input);
     public AutoCompleteServiceMethod ServiceMethod
     {
         get;
         set;
     }
     public string ServiceMethodAssemblyName
     {
         get;
         set;
     }
    }
}
